package com.emmy.walnut.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.emmy.walnut.R;
import com.emmy.walnut.app.AppDatabase;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.model.User;
import com.emmy.walnut.presenter.RegPresenter;
import com.emmy.walnut.presenter.iface.RegPIface;
import com.emmy.walnut.util.PreferenceUtil;
import com.emmy.walnut.view.iface.RegIface;

public class RegisterActivity extends BaseActivity implements RegIface {

    private EditText username;
    private EditText password;
    private EditText again_password;

    private Button register;
    private Button back;

    private RegPIface regPIface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();
    }

    private void init(){
        regPIface = new RegPresenter(this);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        again_password = findViewById(R.id.again_password);

        back = findViewById(R.id.back);
        register = findViewById(R.id.reg_register);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s_username = username.getText().toString();
                String s_password = password.getText().toString();
                String s_again_password = again_password.getText().toString();
                if (s_username.length()==0)
                    Toast.makeText(RegisterActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                else if (s_username.length()<5)
                    Toast.makeText(RegisterActivity.this,"用户名至少五位",Toast.LENGTH_SHORT).show();
                else if(s_password.length()==0)
                    Toast.makeText(RegisterActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                else if(s_again_password.length()==0)
                    Toast.makeText(RegisterActivity.this,"请再次确认密码",Toast.LENGTH_SHORT).show();
                else if (!s_password.equals(s_again_password))
                    Toast.makeText(RegisterActivity.this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show();
                else
                    regPIface.register(s_username, s_password);
            }
        });
    }

    @Override
    public void regSuccess(String uuid, String accessKey) {
        Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, LoginActivity.class));
    }

    @Override
    public void regFailed(String str) {
        Toast.makeText(this,str, Toast.LENGTH_SHORT).show();
        username.setText("");
        password.setText("");
        again_password.setText("");
    }
}
